<?php
/**
 * 今天值得买API接口
 * @author wubaiqing<wubaiqing@vip.qq.com>
 * @copyright Copyright (c) 2013 今天值得买
 * @since 1.5
 */
class ApiController extends Controller
{

	/**
	 * 页面访问权限
	 * @return array 权限列表
	 */
	public function accessRules()
	{
		return array();
	}

	public function actionGetGoodsP($taobaoId)
	{
		header('Content-type: application/json');
        Yii::import('common.extensions.taobao.*');
		$taobao = new Taobao();
		$json = $taobao->getGoods($taobaoId);
		echo 'callback'.'('.json_encode($json).')';
	}

	/**
	 * 手机客户端输出JSON
	 */
	public function actionGetIphone()
	{
		$condition = array();
		$cacheKey = Goods::getGoodsListCacheKey('iphone');
		$data = Yii::app()->cache->get($cacheKey);
		if (!empty($data)) {
			echo json_encode($data);
			Yii::app()->end();
		}

		$now = time();
		$criteria = Goods::getMobileCriteria();
		$goods = Goods::model()->findAll($criteria);
		foreach($goods as $key => $item)
		{
			$rand = rand(300, 1000);
			$data[] = array(
				'_va_content' => $item->id,
				'cat_id' => $item->cat_id,
				'cover_img_url' => $item->picture,
				'cover_imid' => '0',
				'create_time' => $item->created_at,
				'delist_time' => $item->end_time,
				'features' => 0,
				'modify_time' => $item->updated_at,
				'opuid' => '',
				'person_num' => $rand,
				'price' => $item->price,
				'rank' => $item->list_order,
				'ref_cid' => '',
				'ref_id' => $item->tb_id,
				'ref_type' => '',
				'ref_url' => 'http://www.40zhe.com/api/go/id/' . $item->tb_id . '.html',
				'sgid' => $item->id,
				'soldout' => 0,
				'special_price' => $item->origin_price,
				'status' => 1,
				'uid' => $item->tb_id,
				'uname' => $item->title,
				'va_content' => array(
					'content' => '',
					'title' => $item->title
				)
			);
		}

		$data = array('data' => array('rpcret' => array('data' => $data)) );
		Yii::app()->cache->set($cacheKey, $data, 1800);

		echo json_encode($data);
		Yii::app()->end();
	}

	/**
	 * Android手机客户端输出JSON
	 */
	public function actionGetAndroid($catId = 0)
	{
		$condition = array();
		$cacheKey = Goods::getGoodsListCacheKey('android' . $catId);
		$data = Yii::app()->cache->get($cacheKey);

		if (!empty($data)) {
			echo json_encode($data);
			Yii::app()->end();
		}

		$criteria = Goods::getMobileCriteria();
		if ($catId > 0) {
			$criteria->compare('cat_id', '=' . $catId);
		}
		$goods = Goods::model()->findAll($criteria);
		foreach($goods as $key => $item)
		{
			$data[] = array(
				'uid' => $item->id,
				'tb_id' => $item->tb_id,
				'title' => $item->title,
				'cat_id' => $item->cat_id,
				'img_url' => $item->picture,
				'create_time' => $item->created_at,
				'end_time' => $item->end_time,
				'price' => $item->price,
				'origin_price' => $item->origin_price,
				'click_url' => 'http://www.40zhe.com/api/go/id/' . $item->tb_id . '.html',
			);
		}
		Yii::app()->cache->set($cacheKey, $data, 1800);

		echo json_encode($data);
		Yii::app()->end();
	}

	/**
	 * Android手机客户端输出JSON
	 */
	public function actionGetU($catId = 0)
	{
		header("Content-Type:text/html; charset=gbk");
		$condition = array();
		$cacheKey = Goods::getGoodsListCacheKey('get-wubaiqing008' . $catId);
		$data = Yii::app()->cache->get($cacheKey);

		if (!empty($data)) {
			echo $data;
			Yii::app()->end();
		}

		$criteria = Goods::getMobileCriteria();

		if ($catId > 0) {
			$criteria->compare('cat_id', '=' . $catId);
		}
		$criteria->addSearchCondition('t.picture', '40zhe', true, 'NOT LIKE');
		$criteria->addSearchCondition('t.url', 'click', true, 'NOT LIKE');
		$criteria->addSearchCondition('t.picture', 'taobao');
		$criteria->limit = 1000;

		$goods = Goods::model()->findAll($criteria);
		$insertSql = '';
		foreach($goods as $key => $item)
		{
			$data[] = array(
				'uid' => $item->id,
				'tb_id' => $item->tb_id,
				'title' => urlencode($item->title),
				'cat_id' => $item->cat_id,
				'img_url' => $item->picture,
				'start_time' => $item->start_time,
				'end_time' => $item->end_time,
				'price' => $item->price,
				'origin_price' => $item->origin_price,
				'url' => $item->url,
				'list_order' => $item->list_order,
			);
		}
		$data = json_encode($data);
		Yii::app()->cache->set($cacheKey, $data, 1800);
		echo $data;
		Yii::app()->end();
	}

	/**
	 * 手机端分类接口
	 */
	public function actionGetCat()
	{
		$array = array();
		$allCate = Cat::getAllCat();
		foreach ($allCate as $key => $val)
		{
			$array[$val['id']] = $val['name'];
		}
		echo json_encode($array);
	}

	/**
	 * 淘宝客URL跳转
	 * @param integer $id 淘宝客id
	 */
	public function actionGo($id)
	{
		$url = 'http://a.m.tmall.com/i'.$id.'.htm';
		header('Location:' . $url);
		$url = Yii::app()->cache->get(Goods::getGoodsListCacheKey($id));
		if (!empty($url)) {
			header('Location:' . $url);
			Yii::app()->end();
		}
		Yii::import('common.extensions.taobao.*');
		$taobao = new Taobao();
		$url = $taobao->getTaobao($id);
		Yii::app()->cache->set(Goods::getGoodsListCacheKey($id), $url, 86400);
		header('Location:' . $url);
	}

	/**
	 * Android手机客户端输出JSON
	 */
	public function actionWebHtml()
	{
		$condition = array();
		$cacheKey = Goods::getGoodsListCacheKey('web');
		$data = Yii::app()->cache->get($cacheKey);

		if (!empty($data)) {
			Goods::printHtmlApi($data);
			Yii::app()->end();
		}

		$criteria = Goods::getMobileCriteria();
		$goods = Goods::model()->findAll($criteria);

		Goods::printHtmlApi($goods);
		Yii::app()->cache->set($cacheKey, $goods, 1800);
		Yii::app()->end();
	}

	/**
	 * 商品推送接口
	 */
	public function actionGetPushGoods($goodsId)
	{
		$data = file_get_contents('http://www.jtzdm.com/api/getgoods/goodsId/' . $goodsId . '.html');
		$goods = Goods::model()->findByPk($goodsId);
		if (empty($goods)) {
			$goods = new Goods();
		}
		foreach (json_decode($data) as $key => $val) {
			$goods->$key = $val;
		}
		$goods->save();
	}

}
